package logic

import (
	"cart_srv/cart"
	"cart_srv/internal/svc"
	"cart_srv/model/mysql"
	"context"

	"github.com/zeromicro/go-zero/core/logx"
)

type CartListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCartListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartListLogic {
	return &CartListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CartListLogic) CartList(in *cart.CartListRequest) (*cart.CartListResponse, error) {
	// todo: 购物车列表

	c := mysql.CartZye{}

	list, err := c.CartList(int(in.UserId))
	if err != nil {
		logx.Error("列表查询失败")
		return nil, err
	}

	var cartList []*cart.Carts

	for _, v := range list {
		cartList = append(cartList, &cart.Carts{
			GoodsTitle:  v.GoodsTitle,
			GoodsPrice:  float32(v.GoodsPrice),
			GoodsStock:  int64(v.Num),
			GoodsAvatar: v.GoodsAvatar,
			UserId:      int64(v.UserId),
		})
	}

	return &cart.CartListResponse{CartList: cartList}, nil
}
